﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Configuration;
using Table = CrystalDecisions.CrystalReports.Engine.Table;


public partial class ReporteCertificacion : System.Web.UI.Page
{
    public ParameterField crtParamField = new ParameterField();
    public ParameterFields crtParamFields = new ParameterFields();
    public ParameterDiscreteValue paramValue = new ParameterDiscreteValue();
    public ParameterField crtParamField2 = new ParameterField();
    public ParameterFields crtParamFields2 = new ParameterFields();
    public ParameterDiscreteValue paramValue2 = new ParameterDiscreteValue();
    public ReportDocument rep = new ReportDocument();

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            try
            {
                ConnectionInfo connInfo = new ConnectionInfo();
                connInfo.ServerName = ConfigurationManager.AppSettings["ServerName"];
                connInfo.DatabaseName = ConfigurationManager.AppSettings["DatabaseName"];
                connInfo.UserID = ConfigurationManager.AppSettings["UserID"];
                connInfo.Password = ConfigurationManager.AppSettings["Password"];
                rep.Load(Server.MapPath("Reportes\\Certificacion_Ley_espectaculos.rpt"), OpenReportMethod.OpenReportByTempCopy);
                                                   
                CrystalReportViewer1.ReportSource = rep;
                Tables repTbls = rep.Database.Tables;

                paramValue.Value = Session["StrNumeroDocumento"].ToString();//TraerFilasSeleccionadasDelGrid();
                crtParamField.ParameterFieldName = "@ENT_NIT";
                crtParamField.CurrentValues.Add(paramValue);
                crtParamFields.Add(crtParamField);

                paramValue2.Value = Session["StrNumeroRegistro"].ToString();//TraerFilasSeleccionadasDelGrid();
                crtParamField2.ParameterFieldName = "@ENR_REGISTRO";
                crtParamField2.CurrentValues.Add(paramValue2);
                crtParamFields2.Add(crtParamField2);
                foreach (Table repTbl in repTbls)
                {
                    TableLogOnInfo repTblLogonInfo = repTbl.LogOnInfo;
                    repTblLogonInfo.ConnectionInfo = connInfo;
                    repTbl.ApplyLogOnInfo(repTblLogonInfo);
                    repTbl.Location = "SIPA" + ".dbo." + repTbl.Name;
                }


                //CrystalReportViewer1.ReportSource = rep;
                //CrystalReportViewer1.DataBind();
                //CrystalReportViewer1.ParameterFieldInfo = crtParamFields;
                rep.SetParameterValue("@ENT_NIT", Session["StrNumeroDocumento"].ToString());
                rep.SetParameterValue("@ENR_REGISTRO", Session["StrNumeroRegistro"].ToString());


                // Get the report document
                //ReportDocument repDoc = getReportDocument();
                // Stop buffering the response
                Response.Buffer = false;
                // Clear the response content and headers
                Response.ClearContent();
                Response.ClearHeaders();
                try
                {
                    // Export the Report to Response stream in PDF format and file name Customers
                    rep.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "ReporteCertificacion");
                    // There are other format options available such as Word, Excel, CVS, and HTML in the ExportFormatType Enum given by crystal reports
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    ex = null;
                }
                rep.Dispose();
                rep.Close();

                //System.IO.MemoryStream oStream = new System.IO.MemoryStream();
                //oStream = (System.IO.MemoryStream)rep.ExportToStream(ExportFormatType.PortableDocFormat);

                //Response.Clear();
                //Response.Buffer = true;
                //Response.ContentType = @"application/pdf";
                //try
                //{
                //   Response.BinaryWrite(oStream.ToArray());
                //  Response.End();
                //}
                //catch (Exception err)
                //{
                //   Response.Write("< BR >");
                //  Response.Write(err.Message.ToString());
                //}

            }
            catch (Exception ex)
            {
                lblMensaje.Text = ex.Message;
            }
        }
        catch (Exception ex)
        {
            lblMensaje.Text = ex.Message;
        }
    }
    protected void btnBuscar_Click(object sender, EventArgs e)
    {
    }
}